package com.moxi.mogublog.redis.properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * redis配置参数
 *
 * @author JackLi
 * @version 1.0.0
 * @date 2020-05-28 下午10:51
 */
@Component
@Configuration
public class RedisProperties {

    private final static Logger log = LoggerFactory.getLogger(RedisProperties.class);

    @Value("${spring.redis.host}")
    private String host;

    @Value("${spring.redis.port}")
    private Integer port;

    @Value("${spring.redis.password}")
    private String password;

    @Value("${spring.redis.timeout}")
    private Integer timeout;

    @Value("${spring.redis.jedis.pool.min-idle}")
    private Integer minIdle;

    @Value("${spring.redis.jedis.pool.max-idle}")
    private Integer maxIdle;

    @Value("${spring.redis.jedis.pool.max-wait-millis}")
    private Long maxWaitMillis;

    @Value("${spring.redis.database:0}")
    private Integer database;

    @Value("${spring.redis.jedis.pool.max-total}")
    private Integer maxTotal;

    @Value("${spring.redis.jedis.pool.min-evictable-idle-time-millis}")
    private Integer minEvictableIdleTimeMillis;

    /**
     * 对象空闲多久后逐出, 当空闲时间>该值 且 空闲连接>最大空闲数 时直接逐出,不再根据MinEvictableIdleTimeMillis判断  (默认逐出策略)
     */
    @Value("${spring.redis.jedis.pool.soft-min-evictable-idle-time-millis}")
    private long softMinEvictableIdleTimeMillis;

    @Value("${spring.redis.jedis.pool.num-tests-per-eviction-run}")
    private Integer numTestsPerEvictionRun;

    @Value("${spring.redis.jedis.pool.time-between-eviction-runs-rillis}")
    private long timeBetweenEvictionRunsMillis;

    @Value("${spring.redis.jedis.pool.test-on-borrow}")
    private boolean testOnBorrow;

    @Value("${spring.redis.jedis.pool.test-while-idle}")
    private boolean testWhileIdle;

    /**
     * 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true
     */
    @Value("${spring.redis.jedis.pool.block-when-exhausted}")
    private boolean blockWhenExhausted;

    /**
     * 设置的逐出策略类名, 默认DefaultEvictionPolicy(当连接超过最大空闲时间,或连接数超过最大空闲连接数)
     */
    @Value("${spring.redis.jedis.pool.eviction-policy-classname}")
    private String evictionPolicyClassname;

    /**
     * 是否启用pool的jmx管理功能, 默认true
     */
    @Value("${spring.redis.jedis.pool.jmx-enabled}")
    private boolean jmxEnabled;

    /**
     * MBean ObjectName = new ObjectName("org.apache.commons.pool2:type=GenericObjectPool,name=" + "pool" + i); 默认为"pool", JMX不熟,具体不知道是干啥的...默认就好.
     */
    @Value("${spring.redis.jedis.pool.jmx-name-prefix}")
    private String jmxNamePrefix;

    /**
     * 是否启用后进先出, 默认true
     */
    @Value("${spring.redis.jedis.pool.lifo}")
    private boolean lifo;

    @PostConstruct
    private void test() {
        log.error("LiShuai-delete");
        log.error("RedisProperties{" +
                "host='" + host + '\'' +
                ", port=" + port +
                ", password='" + password + '\'' +
                ", timeout=" + timeout +
                ", maxIdle=" + maxIdle +
                ", maxWaitMillis=" + maxWaitMillis +
                ", database=" + database +
                ", maxTotal=" + maxTotal +
                ", minEvictableIdleTimeMillis=" + minEvictableIdleTimeMillis +
                ", softMinEvictableIdleTimeMillis=" + softMinEvictableIdleTimeMillis +
                ", numTestsPerEvictionRun=" + numTestsPerEvictionRun +
                ", timeBetweenEvictionRunsMillis=" + timeBetweenEvictionRunsMillis +
                ", testOnBorrow=" + testOnBorrow +
                ", testWhileIdle=" + testWhileIdle +
                ", blockWhenExhausted=" + blockWhenExhausted +
                ", evictionPolicyClassname='" + evictionPolicyClassname + '\'' +
                ", jmxEnabled=" + jmxEnabled +
                ", jmxNamePrefix='" + jmxNamePrefix + '\'' +
                ", lifo=" + lifo +
                '}');
    }

    public String getHost() {
        return host;
    }

    public Integer getPort() {
        return port;
    }

    public String getPassword() {
        return password;
    }

    public Integer getTimeout() {
        return timeout;
    }

    public Integer getMinIdle() {
        return minIdle;
    }

    public Integer getMaxIdle() {
        return maxIdle;
    }

    public Long getMaxWaitMillis() {
        return maxWaitMillis;
    }

    public Integer getDatabase() {
        return database;
    }

    public Integer getMaxTotal() {
        return maxTotal;
    }

    public Integer getMinEvictableIdleTimeMillis() {
        return minEvictableIdleTimeMillis;
    }

    public long getSoftMinEvictableIdleTimeMillis() {
        return softMinEvictableIdleTimeMillis;
    }

    public Integer getNumTestsPerEvictionRun() {
        return numTestsPerEvictionRun;
    }

    public long getTimeBetweenEvictionRunsMillis() {
        return timeBetweenEvictionRunsMillis;
    }

    public boolean isTestOnBorrow() {
        return testOnBorrow;
    }

    public boolean isTestWhileIdle() {
        return testWhileIdle;
    }

    public boolean isBlockWhenExhausted() {
        return blockWhenExhausted;
    }

    public String getEvictionPolicyClassname() {
        return evictionPolicyClassname;
    }

    public boolean isJmxEnabled() {
        return jmxEnabled;
    }

    public String getJmxNamePrefix() {
        return jmxNamePrefix;
    }

    public boolean isLifo() {
        return lifo;
    }

}
